package com.buba.controller;


import com.buba.mapper.DiaryMapper;
import com.buba.service.DiaryService;
import common.Result;
import entity.DiaryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import result.CommonResult;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/diary")
public class  DiaryController {

    @Autowired
    DiaryService diaryService;

    @Resource
    DiaryMapper diaryMapper;

    /**
     * 日记本列表全查
     * @return
     */
/*    @RequestMapping("getDiaryList")
    public CommonResult getDiary (){
        return CommonResult.ok(diaryService.diaryList());
    }*/

    /**
     * 全查
     * @return
     */
    @RequestMapping("getDiaryList")
    public CommonResult getDiary(){
        List<DiaryEntity> list= diaryService.diaryList();
        return CommonResult.ok(list) ;
    }

    /**
     *
     * 添加日记本列表
     * @param discEntity
     * @return
     */
    @PostMapping("/save")
    public Result sava(@RequestBody DiaryEntity discEntity )  {

        return Result.success(diaryService.save(discEntity));
    }


    /**
     * gaochuang/
     * 2022-11-22
     * 修改日记本
     * @param discEntity
     * @return50
     */
    @PostMapping("/update")
    public Result update(@RequestBody DiaryEntity discEntity ){
        return Result.success(diaryService.updateById(discEntity));
    }

    /**x
     * gaochuang
     * 2022-11-22
     * 查询日记本，
     * @param id
     * @return
     */
    @GetMapping("/DiscList/{id}")
    public Result DiscList(@PathVariable("id") String id){
        return diaryService.diary(id);
    }


    /**x
     * gaochuang
     * 2022-2-23
     * 创建日记本，
     * @return
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody DiaryEntity diaryEntity) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        diaryEntity.setCreateTime(sdf.parse(diaryEntity.getTime()));
        return CommonResult.ok(diaryService.save(diaryEntity));
    }

    /**
     * 追加日记
     * @param diaryEntity
     * @return
     * @throws ParseException
     */
    @PostMapping("/addRelease")
    public CommonResult addRelease(@RequestBody DiaryEntity diaryEntity) throws ParseException{

        diaryEntity.setDiaryTime(diaryEntity.getDiaryTime());
        return CommonResult.ok(diaryService.save(diaryEntity));
    }

    /**
     * 查询 返回当前的时间
     * select time
     * @return
     */
    @GetMapping("/selectTime")
    public Result selectTime(){
        return Result.success(diaryService.selectTime());
    }

}
